package org.learn;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
@RestController
public class IpUpdateApp extends SpringBootServletInitializer {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(IpUpdateApp.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(IpUpdateApp.class);
	}

	@RequestMapping("/update")
	public String update(HttpServletRequest request) {
		String remoteAddr = request.getRemoteAddr();
		request.getServletContext().setAttribute("remoteAddr", remoteAddr);
		return "SUCCESS";
	}
	
	@RequestMapping("/show")
	public String show(HttpServletRequest request) { 
		String remoteAddr  =  (String)request.getServletContext().getAttribute("remoteAddr");
		return remoteAddr;
	}

}